package flc.ast.fragment.edit;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;

import androidx.fragment.app.Fragment;

import com.stark.imgedit.ImgEditActivity;
import com.stark.imgedit.ModuleConfig;
import com.stark.imgedit.view.RotateImageView;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import flc.ast.BaseEditFragment;
import flc.ast.R;
import flc.ast.databinding.FragmentRotateBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link RotateFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class RotateFragment extends BaseEditFragment<FragmentRotateBinding> {

    public static final int INDEX = ModuleConfig.INDEX_ROTATE;
    public RotateImageView mRotateImageView;
    private int currentAngel = 0;
    private Bitmap mImgBitmap;

    public static RotateFragment newInstance() {
        RotateFragment fragment = new RotateFragment();
        return fragment;
    }


    @Override
    public void onShow() {
        mImgEditActivity.mode = ImgEditActivity.MODE_ROTATE;
        clearSelection();
        currentAngel = 0;
        mDataBinding.tvRotate.setTextColor(Color.parseColor("#FFFFFF"));
        mDataBinding.llRotate.setVisibility(View.VISIBLE);
        mImgEditActivity.mRotateImageView.setVisibility(View.VISIBLE);
        mImgBitmap = mImgEditActivity.getMainBit();
        mImgEditActivity.mImgView.setImageBitmap(mImgEditActivity.getMainBit());
        mImgEditActivity.mImgView.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mImgEditActivity.mImgView.setScaleEnabled(false);// 禁用缩放
        mImgEditActivity.mImgView.post(new Runnable() {
            @Override
            public void run() {
                RectF bitmapRect = mImgEditActivity.mImgView.getBitmapRect();
                mImgEditActivity.mRotateImageView.addBit(mImgBitmap, bitmapRect);
            }
        });
    }

    @Override
    public void backToMain() {
        mImgEditActivity.mode = ImgEditActivity.MODE_NONE;
        mImgEditActivity.mRotateImageView.reset();
        mRotateImageView.setVisibility(View.GONE);
        mImgEditActivity.mImgView.setVisibility(View.VISIBLE);
        mImgEditActivity.mImgView.setScaleEnabled(true);// 恢复缩放功能
        mImgEditActivity.showFunc();
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_rotate;
    }

    @Override
    protected void initView() {
        mDataBinding.tvRotate.setOnClickListener(this);
        mDataBinding.tvFlip.setOnClickListener(this);
        mDataBinding.ivLeft90.setOnClickListener(this);
        mDataBinding.ivRight90.setOnClickListener(this);
        mDataBinding.llRotateHorizontal.setOnClickListener(this);
        mDataBinding.llPicRotateVertical.setOnClickListener(this);

        mRotateImageView = mImgEditActivity.mRotateImageView;
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvRotate) {//旋转
            clearSelection();
            mDataBinding.tvRotate.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.llRotate.setVisibility(View.VISIBLE);
        } else if (id == R.id.tvFlip) {//翻转
            clearSelection();
            mDataBinding.tvFlip.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.llFlip.setVisibility(View.VISIBLE);
        } else if (id == R.id.ivLeft90) {//向左90度
            currentAngel -= 90;
            if (currentAngel == 360) {
                currentAngel = 0;
            } else if (currentAngel == 0) {
                currentAngel = 360;
            }
            rotateImg();
        } else if (id == R.id.ivRight90) {//向右90度
            currentAngel += 90;
            if (currentAngel == 360) {
                currentAngel = 0;
            } else if (currentAngel == 0) {
                currentAngel = 360;
            }
            rotateImg();
        } else if (id == R.id.llRotateHorizontal) {//水平
            rotateImg();
            mImgEditActivity.mRotateImageView.mirrorUD(currentAngel);
        } else if (id == R.id.llPicRotateVertical) {//垂直
            rotateImg();
            mImgEditActivity.mRotateImageView.mirrorLR(currentAngel);
        }
    }

    @Override
    protected void initData() {
        onShow();
    }

    //旋转图片
    private void rotateImg() {
        mImgEditActivity.mImgView.setVisibility(View.GONE);
        mImgEditActivity.mRotateImageView.setVisibility(View.VISIBLE);
        mImgEditActivity.mRotateImageView.rotateImage(currentAngel);
    }

    /**
     * 保存旋转图片
     */
    public void applyRotateImage() {
        showDialog(getString(com.stark.common.res.R.string.handling));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                RectF imageRect = mImgEditActivity.mRotateImageView.getImageNewRect();
                Bitmap originBit = Bitmap.createBitmap(mImgBitmap).copy(Bitmap.Config.ARGB_8888, true);
                Bitmap result = Bitmap.createBitmap((int) imageRect.width(), (int) imageRect.height(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(result);
                int w = originBit.getWidth() >> 1;
                int h = originBit.getHeight() >> 1;
                float centerX = imageRect.width() / 2;
                float centerY = imageRect.height() / 2;

                float left = centerX - w;
                float top = centerY - h;

                RectF dst = new RectF(left, top, left + originBit.getWidth(), top + originBit.getHeight());
                canvas.save();
                //bug fixed  应用时不需要考虑图片缩放问题 重新加载图片时 缩放控件会自动填充屏幕
                canvas.scale(mImgEditActivity.mRotateImageView.getScaleX(), mImgEditActivity.mRotateImageView.getScaleY(), imageRect.width() / 2, imageRect.height() / 2);
                canvas.rotate(mImgEditActivity.mRotateImageView.getRotateAngle(), imageRect.width() / 2, imageRect.height() / 2);

                canvas.drawBitmap(originBit, new Rect(0, 0, originBit.getWidth(), originBit.getHeight()), dst, null);
                canvas.restore();
                emitter.onNext(result);
            }

            @Override
            public void accept(Bitmap bitmap) {
                dismissDialog();
                if (bitmap == null) return;
                mImgEditActivity.changeMainBitmap(bitmap, true);
                mImgEditActivity.mCropView.setCropRect(mImgEditActivity.mImgView.getBitmapRect());
                backToMain();
            }
        });
    }

    private void clearSelection() {
        mDataBinding.tvRotate.setTextColor(Color.parseColor("#9C9C9C"));
        mDataBinding.tvFlip.setTextColor(Color.parseColor("#9C9C9C"));
        mDataBinding.llRotate.setVisibility(View.GONE);
        mDataBinding.llFlip.setVisibility(View.GONE);
    }

}